<template>
  <div v-if="isAdd == 1" class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
          <el-form-item label="商品分类" prop="cateId">
            <el-input v-model="queryParams.cateId" placeholder="请输入商品分类" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <!-- TODO 本项目不涉及品牌关联         <el-form-item label="品牌ID" prop="brandId">-->
          <!--            <el-input v-model="queryParams.brandId" placeholder="请输入品牌ID" clearable style="width: 240px" @keyup.enter="handleQuery" />-->
          <!--          </el-form-item>-->
          <el-form-item label="商品名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入商品名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="销售量" prop="sale">
            <el-input v-model="queryParams.sale" placeholder="请输入销售量" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="审核状态" prop="verify">
            <el-input v-model="queryParams.verify" placeholder="请输入审核状态" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card v-if="isAdd == 1" shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['mall:mallGoods:add']" type="primary" plain icon="Plus" @click="handleAdd">新增 </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['mall:mallGoods:remove']" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
              >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['mall:mallGoods:export']" type="warning" plain icon="Download" @click="handleExport">导出 </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="mallGoodsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" width="55" align="center" label="序号" prop="index" />
        <el-table-column align="center" label="ID" prop="id" />
        <el-table-column label="商品名称" align="center" prop="name" width="120" show-overflow-tooltip />
        <el-table-column label="主图" align="center" prop="imgUrl" width="80">
          <template #default="scope">
            <image-preview :src="scope.row.imgUrl" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="品牌" align="center" prop="brandName" />
        <el-table-column label="商品分类" align="center" prop="categoryName" />
        <el-table-column label="价格" align="center" prop="price" />
        <el-table-column label="销售量" align="center" prop="sale" />
        <el-table-column label="收藏量" align="center" prop="collect" />
        <el-table-column label="上架状态" align="center" prop="status">
          <template #default="{ row }">
            <el-switch v-model="row.status" :active-value="0" :inactive-value="1" @change="statusChange(row)" />
          </template>
        </el-table-column>
        <el-table-column label="商家首页" align="center" prop="isRecommend">
          <template #default="{ row }">
            <el-switch v-model="row.isRecommend" :active-value="0" :inactive-value="1" @change="recommendChange(row)" />
          </template>
        </el-table-column>
        <el-table-column label="主站首页" align="center" prop="isMaster">
          <template #default="{ row }">
            <el-switch v-model="row.isMaster" :active-value="0" :inactive-value="1" @change="masterChange(row)" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['mall:mallGoods:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['mall:mallGoods:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
  </div>
  <GoodsAdd v-if="isAdd == 2" :row-data="rowData" @close-add="closeAdd" @get-list="getList" />
</template>

<script setup name="MallGoods" lang="ts">
import { delMallGoods, getMallGoods, listMallGoods, updateMaster, updateRecommend, updateStatus } from '@/api/mall/mallGoods';
import { MallGoodsVO } from '@/api/mall/mallGoods/types';
import GoodsAdd from '@/views/mall/mallGoods/components/Detail.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const rowData = ref({} as MallGoodsVO);
const mallGoodsList = ref<MallGoodsVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const isAdd = ref<number>(1);
const queryFormRef = ref<ElFormInstance>();

const data = reactive<any>({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    brandId: undefined,
    cateId: undefined,
    name: undefined,
    sale: undefined,
    status: undefined,
    verify: undefined,
    contentMobile: undefined,
    isRecommend: undefined,
    isMaster: undefined,
    params: {}
  }
});
const { queryParams } = toRefs(data);
/*关闭新增*/
const closeAdd = (e) => {
  isAdd.value = e;
};
/*===========商品上下架=============*/
const statusChange = async (e: any) => {
  const { code } = await updateStatus(e.id, e.status);
  if (code == 200) {
    proxy?.$modal.msgSuccess('操作成功');
  }
};
/*===========推荐商家首页=============*/
const recommendChange = async (e: any) => {
  const { code } = await updateRecommend(e.id, e.isRecommend);
  if (code == 200) {
    proxy?.$modal.msgSuccess('操作成功');
  }
};
/*===========推荐主站首页=============*/
const masterChange = async (e: any) => {
  const { code } = await updateMaster(e.id, e.isMaster);
  if (code == 200) {
    proxy?.$modal.msgSuccess('操作成功');
  }
};

/** 查询商品列表列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMallGoods(queryParams.value);
  console.log(res);
  mallGoodsList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: MallGoodsVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  isAdd.value = 2;
};

/** 修改按钮操作 */
const handleUpdate = async (row?: MallGoodsVO) => {
  const _id = row?.id || ids.value[0];
  isAdd.value = 2;
  const res = await getMallGoods(_id);
  console.log(res);
  rowData.value = res.data;
};

/** 删除按钮操作 */
const handleDelete = async (row?: MallGoodsVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除商品列表编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delMallGoods(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'mall/mallGoods/export',
    {
      ...queryParams.value
    },
    `mallGoods_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
